home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0076_Delete All Files + Sub-Dirs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-26  |  3.9 KB  |  165 lines

  1. {
  2. From: russchinoy@aol.com (RussChinoy)
  3. >Does anyone have know of where I can get a hold of a routine to delete
  4. >all files off a floppy disk regardless, including all subdirectores?
  5.  
  6. Here's the source to a little DOS utility that I wrote to do exactly what
  7. you are looking for (actually, it's slightly more flexible since you can
  8. specify the starting directory; you can just specify the root), using a
  9. recursive routine.
  10. }
  11.  
  12. {$A-,B-,D-,E-,F-,G-,I-,L-,N-,O-,R-,S-,V-,X+}
  13. {$M 65520, 0, 0}
  14.  
  15. PROGRAM XRD;
  16.  
  17. { This program deletes all files and subdirectories below the specified
  18.   directory, and then removes the specified directory as well.          }
  19.  
  20. USES
  21.     Dos,
  22.     OpDos,
  23.     OpCrt,
  24.     OpString;
  25.  
  26. CONST
  27.      Version = '1.01';
  28.  
  29. VAR
  30.    XRDPath : PathStr;
  31.    f       : file;
  32.  
  33. {*************************************************************************
  34. **}
  35.  
  36. PROCEDURE Init;
  37. VAR
  38.    TempPath : PathStr;
  39.    c        : char;
  40. BEGIN
  41. writeln;
  42. writeln('XRD, Version ', Version, ', Copyright (c) 1992 RC Software');
  43. writeln;
  44. IF paramcount <> 1 THEN
  45.    BEGIN
  46.    writeln('   Purpose:  XRD removes the specified directory, and');
  47.    writeln('             all files and subdirectories under it.');
  48.    writeln;
  49.    writeln('   Syntax:   XRD [d:]<path>');
  50.    writeln;
  51.    writeln('   Where:    d: is an optional drive specifier, and');
  52.    writeln('             <path> is the path to be removed.');
  53.    writeln;
  54.    halt(1);
  55.    END;
  56.  
  57. XRDPath := paramstr(1);
  58.  
  59. IF NOT IsDirectory(XRDPath) THEN
  60.    BEGIN
  61.    writeln('Error: specified path not found.');
  62.    writeln;
  63.    halt(2);
  64.    END;
  65.  
  66. XRDPath := fexpand(XRDPath);
  67.  
  68. IF XRDPath[length(XRDPath)] <> '\'
  69.    THEN XRDPath := XRDPath + '\';
  70.  
  71. XRDPath := StUpCase(XRDPath);
  72.  
  73. TempPath := copy(XRDPath, 1, length(XRDPath) - 1);
  74.  
  75. writeln('WARNING!!  You are about to remove the following directory');
  76. writeln('           (and delete all files and subdirectories under it):');
  77. writeln('           '#16' ', TempPath, ' ', #17);
  78. write('Continue (Y/N)? ');
  79.  
  80. REPEAT
  81. c := upcase(readkey);
  82. IF c = #0 THEN
  83.    BEGIN
  84.    c := readkey;
  85.    c := #255;
  86.    END;
  87. UNTIL c in ['Y', 'N'];
  88. writeln(c);
  89. writeln;
  90.  
  91. IF c = 'N' THEN halt(3);
  92. END;
  93.  
  94. {*************************************************************************
  95. **}
  96.  
  97. PROCEDURE DeleteAllFilesInDir(DirName : PathStr);
  98. VAR
  99.    s : searchrec;
  100. BEGIN
  101. FindFirst(DirName + '*.*', AnyFile, s);
  102. WHILE DosError = 0 DO
  103.       BEGIN
  104.       IF (s.Attr <> Directory) AND
  105.          (s.name <> '.') AND
  106.          (s.name <> '..')
  107.          THEN BEGIN
  108.               gotoxy(1, wherey);
  109.               clreol;
  110.               write('Deleting: ', DirName + s.name);
  111.               Assign(f, DirName + s.name);
  112.               erase(f);
  113.               IF IOResult <> 0 THEN
  114.                  BEGIN
  115.                  SetFAttr(f, Archive);
  116.                  erase(f);
  117.                  IF IOResult <> 0 THEN
  118.                    BEGIN
  119.                    gotoxy(1, wherey);
  120.                    clreol;
  121.                    writeln('Error: unable to delete ', DirName + s.name);
  122.                    END;
  123.                  END;
  124.               END
  125.          ELSE IF (s.Attr = Directory) AND
  126.                  (s.name <> '.') AND
  127.                  (s.name <> '..')
  128.                  THEN DeleteAllFilesInDir(DirName + s.name + '\');
  129.       FindNext(s);
  130.       END;
  131.  
  132. IF (length(DirName) > 1) AND
  133.    NOT ((length(DirName) = 3) AND (DirName[3] = '\')) THEN
  134.    BEGIN
  135.    gotoxy(1, wherey);
  136.    clreol;
  137.    write('Removing: ', copy(DirName, 1, length(DirName) - 1));
  138.  
  139.    rmdir(copy(DirName, 1, length(DirName) - 1));
  140.  
  141.    IF IOResult <> 0 THEN
  142.       BEGIN
  143.       gotoxy(1, wherey);
  144.       clreol;
  145.       writeln('Error: unable to remove ', copy(DirName, 1, length(DirName)
  146. - 1));
  147.       END;
  148.    END;
  149. END;
  150.  
  151. {*************************************************************************
  152. **}
  153.  
  154. BEGIN
  155. Init;
  156.  
  157. DeleteAllFilesInDir(XRDPath);
  158.  
  159. gotoxy(1, wherey);
  160. clreol;
  161. writeln('Done.');
  162. writeln;
  163. END.
  164.  
  165.